home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gawk / cawf2st.zoo / pass3.c < prev    next >
C/C++ Source or Header  |  1992-04-12  |  21KB  |  579 lines

  1. /*
  2.  *      pass3.c - cawf(1) pass 3 function
  3.  */
  4.  
  5. /*
  6.  *      Copyright (c) 1991 Purdue University Research Foundation,
  7.  *      West Lafayette, Indiana 47907.  All rights reserved.
  8.  *
  9.  *      Written by Victor A. Abell <abe@mace.cc.purdue.edu>,  Purdue
  10.  *      University Computing Center.  Not derived from licensed software;
  11.  *      derived from awf(1) by Henry Spencer of the University of Toronto.
  12.  *
  13.  *      Permission is granted to anyone to use this software for any
  14.  *      purpose on any computer system, and to alter it and redistribute
  15.  *      it freely, subject to the following restrictions:
  16.  *
  17.  *      1. The author is not responsible for any consequences of use of
  18.  *         this software, even if they arise from flaws in it.
  19.  *
  20.  *      2. The origin of this software must not be misrepresented, either
  21.  *         by explicit claim or by omission.  Credits must appear in the
  22.  *         documentation.
  23.  *
  24.  *      3. Altered versions must be plainly marked as such, and must not
  25.  *         be misrepresented as being the original software.  Credits must
  26.  *         appear in the documentation.
  27.  *
  28.  *      4. This notice may not be removed or altered.
  29.  */
  30.  
  31. #include "cawf.h"
  32.  
  33. void
  34. Pass3(len, word, sarg, narg)
  35.         int len;                        /* length (negative is special) */
  36.         char *word;                     /* word */
  37.         char *sarg;                     /* string argument */
  38.         int narg;                       /* numeric argument */
  39. {
  40.         int addto;                      /* spaces to add to all words */
  41.         int i, j, k;                    /* temporary index */
  42.         char msg[MAXLINE];              /* message buffer */
  43.         int n;                          /* temporary number */
  44.         char *s1;                       /* temporary string pointer */
  45.         int wl;                         /* real word length */
  46.         int xsp;                        /* extra spaces to add */
  47.         int vsp;                        /* vertical spacing status */
  48.  
  49.         vsp = 0;
  50.         if (word != NULL)
  51.                 wl = strlen(word);
  52.     /*
  53.      * If not a special command, process a word.
  54.      */
  55.         if (len >= 0 && Outll < 0) {
  56.         /*
  57.          * Enter first word.
  58.          */
  59.                 (void) strcpy(Outln, word);
  60.                 Outll = len;
  61.                 Outlx = wl;
  62.                 Padx = 0;
  63.         } else if (len >= 0
  64.                && (Outll+Contlen+len+narg) <= (LL-Pgoff-Ind-Tind)) {
  65.         /*
  66.          * The word fits, so enter it.
  67.          */
  68.                 if ((Contlen + len) > 0) {
  69. line_too_big:
  70.                         if ((Outlx + Contlen + wl) >= MAXOLL) {
  71.                                 Error3(len, word, sarg, narg,
  72.                                         "output line too big");
  73.                                 return;
  74.                         } else {
  75.                                 if (Contlen > 0 && Cont != NULL) {
  76.                                         if (Contlen == 1 && *Cont == ' ') {
  77.                                                 Padchar[Padx++] = Outlx;
  78.                                                 Outln[Outlx++] = ' ';
  79.                                         } else {
  80.                                                 (void) strcpy(&Outln[Outlx],
  81.                                                         Cont);
  82.                                                 Outlx += Contlen;
  83.                                         }
  84.                                 }
  85.                                 if (len > 0) {
  86.                                         (void) strcpy(&Outln[Outlx], word);
  87.                                         Outlx += wl;
  88.                                 }
  89.                         }
  90.                 }
  91.                 Outll += Contlen + len;
  92.         } else if (len == NOBREAK || len == MESSAGE) {
  93.                 /*
  94.                  * Do nothing (equivalent to break)
  95.                  */
  96.         } else if (len == DOBREAK && strcmp(word, "need") == 0
  97.                &&  (Nxtln + narg) < (Pglen + 1 - Botmarg)) {
  98.                 /*
  99.                  * Do nothing, because there is room on the page.
  100.                  */
  101.         } else if (len == DOBREAK && strcmp(word, "toindent") == 0
  102.                &&  (Ind + Tind + Outll) < Ind) {
  103.         /*
  104.          * Move to indent position with line - there is room.
  105.          */
  106.                 n = Ind - (Ind + Tind +Outll);
  107.                 Outll += n;
  108.                 if ((Outlx + n) >= MAXOLL)
  109.                         goto line_too_big;
  110.                 for (i = n; i; i--)
  111.                         Outln[Outlx++] = ' ';
  112.                 Padx = 0;
  113.                 Free(&Cont);
  114.                 Contlen = 0;
  115.         } else if (Outll >= 0
  116.                || (len == DOBREAK && strcmp(word, "need") == 0)) {
  117.         /*
  118.          * A non-empty line or a "need" forces output.
  119.          */
  120.                 vsp = 0;
  121.  
  122. print_line:
  123.                 if (Nxtln == 1) {
  124.             /*
  125.              * We're at the top of the page, so issue the header.
  126.              */
  127.                         if (Thispg > 1)
  128.                                 Charput('\f');
  129.                         for (i = (Topmarg - 1)/2; i > 0; i--) {
  130.                                 Charput('\n');
  131.                                 Nxtln++;
  132.                         }
  133.                     /*
  134.                      * Print the page header, as required.
  135.                      */
  136.                         if (Fph || Thispg > 1) {
  137.                                 i = LenprtHF(Hdc, Thispg, 0)
  138.                                   + LenprtHF(Hdl, Thispg, 0)
  139.                                   + LenprtHF(Hdr, Thispg, 0) + 2;
  140.                                 j = (LL - i - Pgoff) / 2 + 1;
  141.                                 n = LL - Pgoff - i - j + 2;
  142.                                 for (k = 0; k < Pgoff; k++)
  143.                                         Charput(' ');
  144.                                 if (Hdl)
  145.                                         LenprtHF(Hdl, Thispg, 1);
  146.                                 while (j--)
  147.                                         Charput(' ');
  148.                                 if (Hdc)
  149.                                         LenprtHF(Hdc, Thispg, 1);
  150.                                 while (n--)
  151.                                         Charput(' ');
  152.                                 if (Hdr)
  153.                                         LenprtHF(Hdr, Thispg, 1);
  154.                                 Charput('\n');
  155.                         } else
  156.                                 Charput('\n');
  157.                         Nxtln++;
  158.                         while(Nxtln <= Topmarg) {
  159.                                 Charput('\n');
  160.                                 Nxtln++;
  161.                         }
  162.                 }
  163.             /*
  164.              *  Add a trailing hyphen, if mecessary.
  165.              */
  166.                 if (vsp == 0 && Eollen > 0 && Eol != NULL) {
  167.                         i = strlen(Eol);
  168.                         if ((Outlx + i) >= MAXOLL)
  169.                                 goto line_too_big;
  170.                         (void) strcpy(&Outln[Outlx], Eol);
  171.                         Outlx += i;
  172.                         Outll += Eollen;
  173.                 }
  174.             /*
  175.              * Trim trailing spaces from the output line.
  176.              */
  177.                 while (Outlx > 0) {
  178.                         if (Outln[Outlx - 1] != ' ')
  179.                                 break;
  180.                         if (Padx > 0 && (Outlx - 1) == Padchar[Padx - 1])
  181.                                 Padx--;
  182.                         Outlx--;
  183.                         Outln[Outlx] = '\0';
  184.                         Outll--;
  185.                 }
  186.                 if (Outlx == 0)
  187.                         Charput('\n');
  188.                 else if (len == DOBREAK && strcmp(word, "center") == 0) {
  189.                     /*
  190.                      * Center the output line.
  191.                      */
  192.                         i = (LL - Pgoff - Outll) / 2;
  193.                         if (i < 0)
  194.                                 i = 0;
  195.                         for (j = (Pgoff + Ind + Tind + i); j; j--)
  196.                                 Charput(' ');
  197.                         Stringput(Outln);
  198.                         Charput('\n');
  199.                 } else if